<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>查看试题</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layuimoduls/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuimoduls/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索试题</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">试题科目</label>
                            <div class="layui-input-inline">
                                <input type="text" name="sex" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">试题类型</label>
                            <div class="layui-input-inline">
                                <input type="text" name="city" autocomplete="off" class="layui-input">
                            </div>
                        </div>
<!--                        <div class="layui-inline">-->
<!--                            <label class="layui-form-label"></label>-->
<!--                            <div class="layui-input-inline">-->
<!--                                <input type="text" name="classify" autocomplete="off" class="layui-input">-->
<!--                            </div>-->
<!--                        </div>-->
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加试题 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 组织试卷 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

<!--        列工具条-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/layuimoduls/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script  th:inline="none">
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/getquestionsTable',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox",},
                {field: 'questionid', title: 'ID', sort: true},
                {field: 'stem', title: '题干',},
                {field: 'opta', title: '选项A'},
                {field: 'optb', title: '选项B'},
                {field: 'optc', title: '选项C'},
                {field: 'optd', title: '选项D'},
                {field: 'right', title: '答案'},
                {field: 'analyse', title: '解析'},
                {field: 'type', title: '类型'},
                {field: 'subjectname', title: '科目'},
                {field: 'score', title: '分值', sort: true},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 10, 20],
            limit: 10,
            page: true,
            skin: 'line',
            //分页
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                var result;
                console.log(this);
                console.log(JSON.stringify(res));
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                }
            }
        })
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听头部工具栏事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加试题操作
                var index = layer.open({
                    title: '添加试题',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/addquestion',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else var data;
            if (obj.event === 'delete') {  // 监听组织试卷操作
                var checkStatus = table.checkStatus('currentTableId')
                var data = JSON.stringify(checkStatus.data)
                var index = layer.open({
                    title: '组织试卷',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/addexam',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改试题',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/page/updatequestion',
                    yes:function(index,layero){
                        //回调的数值
                        var stem = $(layero).find('iframe')[0].contentWindow.stem.value;
                        var opta = $(layero).find('iframe')[0].contentWindow.getElementById('opta').value;
                        var optb = $(layero).find('iframe')[0].contentWindow.getElementById('optb').value;
                        var optc = $(layero).find('iframe')[0].contentWindow.getElementById('optc').value;
                        var optd = $(layero).find('iframe')[0].contentWindow.getElementById('optd').value;
                        var right = $(layero).find('iframe')[0].contentWindow.getElementById('right').value;
                        var analyse = $(layero).find('iframe')[0].contentWindow.getElementById('analyse').value;
                        var score = $(layero).find('iframe')[0].contentWindow.getElementById('score').value;
                        //同步数据表中的数据
                        obj.update({
                            'stem':stem,
                            'opta':opta,
                            'optb':optb,
                            'optc':optc,
                            'optd':optd,
                            'right':right,
                            'analyse':analyse,
                            'score':score,
                        })
                        layer.close(index);
                    },
                    //获取源数据显示在更新框中
                    success:function(layero,index){
                        var div = layero.find('iframe').contents().find('#form');
                        var body = layer.getChildFrame('body',index);
                        var iframeWindow = window['layui-layer-iframe'+index]
                        body.find('#questionid').val(data.questionid);
                        body.find('#stem').val(data.stem);
                        body.find('#opta').val(data.opta);
                        body.find('#optb').val(data.optb);
                        body.find('#optd').val(data.optd);
                        body.find('#optc').val(data.optc);
                        body.find('#right').val(data.right);
                        body.find('#analyse').val(data.analyse);
                        body.find('#score').val(data.score);
                    }
                });

                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }

            else if (obj.event === 'delete') {
                console.log(data)
                var tip = layer.confirm(
                    '真的删除id为'+data.questionid+'的试题吗？',
                    {btn:['确定删除','取消']},
                    function () {
                    $.post({
                        url: "/deleteQuestion",
                        data: JSON.stringify(data),
                        type: 'post',
                        contentType: 'application/json;charset=utf-8',
                        dataType: 'json',
                        success: function (response) {
                            if (response.success) {
                                obj.del();
                                layer.msg(response.msg)
                                layer.close(tip);
                            } else {
                                layer.msg(response.msg)
                            }
                        }
                    })
                },
                    function () {
                        layer.close(tip);
                    }
                );
            }

        });


    });
</script>

</body>
</html>